package com.mengwen.backstage.controller.partner;

import com.mengwen.backstage.pojo.Invitation;
import com.mengwen.backstage.pojo.Result;
import com.mengwen.backstage.service.InvitationService;
import com.mengwen.backstage.service.PartnerService;
import com.mengwen.backstage.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @author LiangYonghui
 * @date 2020/4/19 15:12
 * @description 合伙人团队数据controller
 */
@Controller
@Slf4j
public class PartnerTeamController {

    @Autowired
    PartnerService partnerService;

    @Autowired
    InvitationService invitationService;

    /**
     * 根据邀请者的inviterUnionid
     * @param request
     * @param inviterUnionid
     * @return
     */
    @ResponseBody
    @RequestMapping("/partner/add_teammate_count/{inviter_unionid}")
    public Result addTeammateCount(HttpServletRequest request,@PathVariable("inviter_unionid") String inviterUnionid){
        String token = request.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        String unionid = (String) claims.get("unionid");
        String type = (String) claims.get("type");

        // 校验权限pc端的token，看是否具有权限
        if (type.equals("type") && ! (null == unionid) ) {
            String foundinviterUnionid = invitationService.findInviterUnionidByInviteeUnionid(unionid);
            if (inviterUnionid.equals(foundinviterUnionid)) {
                int count = partnerService.addTeammateCountByUnionid(inviterUnionid);
                if (count == 1){
                    return new Result(false,"更新团队人数成功");
                } else {log.info("数据库操作原因的更新团队人数失败。");}

            }else {
                log.info("邀请者对的unionid与传入的不一致，无法更新。传入：" + inviterUnionid + "，本人：" + unionid );
            }


        }
        return new Result(false,"更新团队人数失败");
    }




    /**
     * 根据邀请者的inviterUnionid
     * @param request
     * @param inviterUnionid
     * @return
     */
    @ResponseBody
    @RequestMapping("/partner/add_publish_count/{inviter_unionid}")
    public Result addPublishCount(HttpServletRequest request, @PathVariable("inviter_unionid") String inviterUnionid){
        String token = request.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        String unionid = (String) claims.get("unionid");
        String type = (String) claims.get("type");

        // 校验权限pc端的token，看是否具有权限
        if (type.equals("type") && ! (null == unionid) ) {
            // 查询数据库中的邀请者的unionid
            String foundinviterUnionid = invitationService.findInviterUnionidByInviteeUnionid(unionid);

            if (inviterUnionid.equals(foundinviterUnionid)) {
                int count = partnerService.addPublishCountByUnionid(inviterUnionid);
                if (count == 1) {
                    return new Result(true,"更新团队发布数量成功");

                }else {
                    log.info("数据库操作原因的更新团队发布数量失败。");
                    return new Result(false,"更新团队发布数量失败");
                }

            }else {

                log.info("邀请者对的unionid与传入的不一致，无法更新。传入：" + inviterUnionid + "，本人：" + unionid );
            }
            return new Result(false,"更新团队发布数量失败");

        }
        return new Result(false,"更新团队发布数量失败");
    }
}
